package com.rerun.controller.system;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.Log;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.SysDept;
import com.rerun.service.ISysDeptService;

/**
 * 部门控制器
 *
 * @author Gaojun.Zhou
 * @date 2016年12月16日 下午5:03:49
 */
@Controller
@RequestMapping("/system/dept")
public class DeptController extends SuperController{  

	@Autowired private ISysDeptService sysDeptService;

    /**
     * 分页查询部门
     *
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @param search     the search
     * @param model      the model
     * @return the string
     */
    @RequiresPermissions("listDept")
    @RequestMapping("/list/{pageNumber}")  
    public  String list(@PathVariable Integer pageNumber,@RequestParam(defaultValue="15") Integer pageSize, String search,Model model){
    	
		Page<SysDept> page = getPage(pageNumber,pageSize);
		model.addAttribute("pageSize", pageSize);
		// 查询分页
		EntityWrapper<SysDept> ew = new EntityWrapper<SysDept>();
		if(StringUtils.isNotBlank(search)){
			ew.like("deptName",search);
			model.addAttribute("search",search);
		}
		Page<SysDept> pageData = sysDeptService.selectPage(page, ew);
		model.addAttribute("pageData", pageData);
		return "system/dept/list";
    }

    /**
     * 新增部门
     *
     * @param model the model
     * @return the string
     */
    @RequiresPermissions("addDept")
    @RequestMapping("/add")  
    public  String add(Model model){
		return "system/dept/add";
    }

    /**
     * 执行新增
     *
     * @param dept   the dept
     * @param roleId the role id
     * @return the rest
     */
    @RequiresPermissions("addDept")
    @Log("创建部门")
    @RequestMapping("/doAdd")  
	@ResponseBody
    public  Rest doAdd(SysDept dept,String[] roleId){
    	
    	sysDeptService.insert(dept);
		return Rest.ok();
    }

    /**
     * 删除部门
     *
     * @param id the id
     * @return the rest
     */
    @RequiresPermissions("deleteDept")
    @Log("删除部门")
    @RequestMapping("/delete")  
    @ResponseBody
    public  Rest delete(String id){
    	sysDeptService.deleteById(id);
    	return Rest.ok();
    }

    /**
     * 编辑部门
     *
     * @param id    the id
     * @param model the model
     * @return the string
     */
    @RequiresPermissions("editDept")
    @RequestMapping("/edit/{id}")  
    public  String edit(@PathVariable String id,Model model){
    	SysDept dept = sysDeptService.selectById(id);

    	model.addAttribute("dept",dept);
    	return "system/dept/edit";
    }

    /**
     * 执行编辑
     *
     * @param dept  the dept
     * @param model the model
     * @return the rest
     */
    @RequiresPermissions("editDept")
    @Log("编辑部门")
    @RequestMapping("/doEdit")  
	@ResponseBody
    public  Rest doEdit(SysDept dept,Model model){
    	sysDeptService.updateById(dept);
    	return Rest.ok();
    } 
	
}
